﻿using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;
using GitNet.WindowsApp.ViewModels;

namespace GitNet.WindowsApp.AddViews;

public partial class CloneView : UserControl
{
    public CloneView()
    {
        InitializeComponent();
        UrlBox = this.FindControl<TextBox>("UrlBox");
        PathBox = this.FindControl<TextBox>("PathBox");
    }

    private void InitializeComponent()
    {
        AvaloniaXamlLoader.Load(this);
    }

    private async void PathChooseClick(object? sender, RoutedEventArgs e)
    {
        var dialog = new OpenFolderDialog();
        var result = await dialog.ShowAsync(new Window());
        if(string.IsNullOrEmpty(result))return;
        PathBox.Text = result;
    }

    private void OkClick(object? sender, RoutedEventArgs e)
    {
        if(Parent is not Control control)return;
        if(control.DataContext is not MainWindowViewModel model)return;
        if(string.IsNullOrEmpty(UrlBox.Text) || string.IsNullOrEmpty(PathBox.Text))return;
        model.Clone(UrlBox.Text,PathBox.Text);
    }
}